package com.xlro.service;

import com.xlro.domain.Admin;
import com.xlro.domain.User;
import com.xlro.dao.LoginDao;

import javax.servlet.http.HttpSession;

public class LoginService {

    private LoginDao loginDao = new LoginDao();

    public String login(String username, String password, String role, HttpSession session) {

        if("1".equals(role)){
            User user = loginDao.selectOne(username);
            if( user == null ){
                return "用户不存在";
            }else{
                if(password.equals(user.getPassword())){
                    session.setAttribute("user",user);
                    session.setAttribute("isLogin",true);
                    return "登陆成功";
                }else{
                    return "密码错误";
                }
            }
        }else{
            Admin admin = loginDao.selectAdmin(username);
            if( admin == null ){
                return "用户不存在";
            }else{
                if(password.equals(admin.getPassword())){
                    session.setAttribute("user",admin);
                    session.setAttribute("isLogin",true);
                    return "登陆成功";
                }else{
                    return "密码错误";
                }
            }
        }
    }
}
